// Copyright (C) 2023 即时通讯网(52im.net) & Jack Jiang.
// The RainbowChat Project. All rights reserved.
// 
// 【本产品为著作权产品，合法授权后请放心使用，禁止外传！】
// 【本次授权给：<北京小羊驼科技有限公司>，授权编号：<NT231212144350>，代码指纹：<A.702363430.550>，技术对接人微信：<ID: wxid_wbyootvkdcgj22>】
// 【授权寄送：<收件：苏先生、地址：北京市大兴区北京密码西区6号楼1单元301、电话：18613885610、邮箱：bd@huiyuanxiang-inc.com>】
// 
// 【本系列产品在国家版权局的著作权登记信息如下】：
// 1）国家版权局登记名(简称)和权证号：RainbowChat    （证书号：软著登字第1220494号、登记号：2016SR041877）
// 2）国家版权局登记名(简称)和权证号：RainbowChat-Web（证书号：软著登字第3743440号、登记号：2019SR0322683）
// 3）国家版权局登记名(简称)和权证号：RainbowAV      （证书号：软著登字第2262004号、登记号：2017SR676720）
// 4）国家版权局登记名(简称)和权证号：MobileIMSDK-Web（证书号：软著登字第2262073号、登记号：2017SR676789）
// 5）国家版权局登记名(简称)和权证号：MobileIMSDK    （证书号：软著登字第1220581号、登记号：2016SR041964）
// * 著作权所有人：江顺/苏州网际时代信息科技有限公司
// 
// 【违法或违规使用投诉和举报方式】：
// 联系邮件：jack.jiang@52im.net
// 联系微信：hellojackjiang
// 联系QQ号：413980957
// 授权说明：http://www.52im.net/thread-1115-1-1.html
// 官方社区：http://www.52im.net
package com.x52im.rainbowchat;

import java.text.MessageFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.eva.epc.common.file.FileHelper;
import com.eva.epc.common.util.CommonUtils;
import com.eva.epc.common.util.Platform;
import com.eva.epc.common.util.PropertyMan;
import com.eva.framework.utils.LoggerFactory;

public class BaseConf
{
	private PropertyMan pm = null;
	
	private final static String TAG = BaseConf.class.getSimpleName();
	private static BaseConf instance = null;
	
	/** 邀请好友邮件模板 */
	private String SNS_INVITE_MAIL_CONTENT_TEMPLATE = null;
	/** 服务端响应用户“忘记密码”请求的邮件模板 */
	private String SNS_RESPONSE_FOR_FORGOTPSW_TEMPLATE = null;
	/** 服务端响应用户“忘记密码”请求（没有查到账户信息）的邮件模板 */
	private String SNS_RESPONSE_FOR_FORGOTPSW_ERROR_TEMPLATE = null;
	
	public static BaseConf getInstance()
	{
		if(instance == null)
			instance = new BaseConf();
		return instance;
	}
	
	public BaseConf()
	{
//		String path = "/E:/Program Files/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/RobotimeMissU_s_new/WEB-INF/classes/com/robotime/missu_s/ends/base_conf.properties";
		String pathOriginal = BaseConf.class.getResource("base_conf.properties").getPath();
		String path = Platform.isWindows()?pathOriginal.substring(1):pathOriginal;
		
		LoggerFactory.getLog().debug("["+TAG+"] RainbowChat服务端的核心配置文件路径："+path);
		
		pm = new PropertyMan(path);
	}
	
	/**
	 * TODO 本方法以后可考虑收纳到父类中！
	 * 因Properties文件默认机制是采用ISO8859-1处理的，所以中文字符读取会存在乱码现象。
	 * 本方法是在父类方法的基础上解决乱码问题。
	 * 
	 * @param key
	 * @return
	 */
	public String getPropertyUTF(String key)
	{
		try
		{
			String s = pm.getProperty(key);
			return new String(s.getBytes("ISO-8859-1"), "utf-8");
		}
		catch (Exception e)
		{
			LoggerFactory.getLog().error("["+TAG+"] RainbowChat的配置项："+key
					+" 读取失败，请检查您的配置文件路径是否放置正确！", e);
			return null;
		}
	}
	
	//------------------------------------------------------------ 邀请好友时的邮件配置
	public String getSNS_INVITE_SMTP_SERVER()
	{
		return getPropertyUTF("SNS_INVITE_SMTP_SERVER");
	}
	public String getSNS_INVITE_SMTP_PORT()
	{
		return getPropertyUTF("SNS_INVITE_SMTP_PORT");
	}
	public boolean getSNS_INVITE_SMTP_SSL()
	{
		return Boolean.valueOf(getPropertyUTF("SNS_INVITE_SMTP_SSL"));
	}
	public String getSNS_INVITE_MASTER_EMAIL_ADDR()
	{
		return getPropertyUTF("SNS_INVITE_MASTER_EMAIL_ADDR");
	}
	public String getSNS_INVITE_MASTER_EMAIL_NAME()
	{
		return getPropertyUTF("SNS_INVITE_MASTER_EMAIL_NAME");
	}
	public String getSNS_INVITE_MASTER_EMAIL_PASSWORD()
	{
		return getPropertyUTF("SNS_INVITE_MASTER_EMAIL_PASSWORD");
	}
	public String getSNS_INVITE_MASTER_NICK_NAME()
	{
		return getPropertyUTF("SNS_INVITE_MASTER_NICK_NAME");
	}
	public String getSNS_INVITE_MAIL_SUBJECT()
	{
		return getPropertyUTF("SNS_INVITE_MAIL_SUBJECT");
	}
	
	public String getSNS_RESPONSE_FORGOT_PSW_MAIL_SUBJECT()
	{
		return getPropertyUTF("SNS_RESPONSE_FORGOT_PSW_MAIL_SUBJECT");
	}
	public String getSNS_RESPONSE_FORGOT_PSW_ERROR_MAIL_SUBJECT()
	{
		return getPropertyUTF("SNS_RESPONSE_FORGOT_PSW_ERROR_MAIL_SUBJECT");
	}
	
	/**
	 * 返回好友邀请邮件正文的模板.
	 * 
	 * @return
	 */
	private String getSNS_INVITE_MAIL_CONTENT_TEMPLATE()
	{
		try
		{
			// 缓存起来，提升性能
			if(SNS_INVITE_MAIL_CONTENT_TEMPLATE == null)
				SNS_INVITE_MAIL_CONTENT_TEMPLATE = FileHelper.readStringFromFile2(
					BaseConf.class.getResource("invite_template.html").toURI(), "utf-8");
		}
		catch(Exception e)
		{
			LoggerFactory.getLog().warn(e.getMessage(), e);
		}
		
		return SNS_INVITE_MAIL_CONTENT_TEMPLATE;
	}
	/**
	 * 返回填充了内容之后的好友邀请邮件正文.
	 * 
	 * @param fromNickName 目前getSNS_INVITE_MAIL_CONTENT_TEMPLATE()中的模板第0个参数是：发起人的昵称
	 * @param fronMail 模板第1个参数是：发起人的mail地址
	 * @param fromUid 模板第2个参数是：发起人的uid
	 * @return
	 */
	public String getSNS_INVITE_MAIL_CONTENT(String fromNickName, String fronMail, String fromUid)
	{
		String contentTemplate = getSNS_INVITE_MAIL_CONTENT_TEMPLATE();
		if(contentTemplate != null)
			return MessageFormat.format(contentTemplate,fromNickName,fronMail,fromUid);
		return null;
	}
	
	/**
	 * 返回服务端响应用户“忘记密码”请求的邮件模板.
	 * 
	 * @return
	 */
	private String getSNS_RESPONSE_FOR_FORGOTPSW_TEMPLATE()
	{
		try
		{
			// 缓存起来，提升性能
			if(SNS_RESPONSE_FOR_FORGOTPSW_TEMPLATE == null)
			{
				SNS_RESPONSE_FOR_FORGOTPSW_TEMPLATE = FileHelper.readStringFromFile2(
					BaseConf.class.getResource("response_for_forgotpsw_template.html").toURI());
			}
		}
		catch(Exception e)
		{
			LoggerFactory.getLog().warn(e.getMessage(), e);
		}
		
		return SNS_RESPONSE_FOR_FORGOTPSW_TEMPLATE;
	}
	/**
	 * 返回填充了内容之后的服务端响应用户“忘记密码”请求的邮件模板.
	 * 
	 * @param nickName 目前getSNS_RESPONSE_FOR_FORGOTPSW_TEMPLATE()中的模板第0个参数是：昵称
	 * @param uid 模板第2个参数是：uid
	 * @param mail 模板第3个参数是：mail地址
	 * @param password 模板第4个参数是：用户登陆密码
	 * @return
	 */
	public String getSNS_RESPONSE_FOR_FORGOTPSW_TEMPLATE(String nickName, String uid, String mail, String password, String path)
	{
		String contentTemplate = getSNS_RESPONSE_FOR_FORGOTPSW_TEMPLATE();
		if(contentTemplate != null)
			return MessageFormat.format(contentTemplate
					,nickName,new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()), uid, mail, password, path);
		return null;
	}
	
	/**
	 * 返回服务端响应用户“忘记密码”请求（没有查到账户信息）的邮件模板.
	 * 
	 * @return
	 */
	private String getSNS_RESPONSE_FOR_FORGOTPSW_ERROR_TEMPLATE()
	{
		try
		{
			// 缓存起来，提升性能
			if(SNS_RESPONSE_FOR_FORGOTPSW_ERROR_TEMPLATE == null)
			{
				SNS_RESPONSE_FOR_FORGOTPSW_ERROR_TEMPLATE = FileHelper.readStringFromFile2(
					BaseConf.class.getResource("response_for_forgotpsw_error_template.html").toURI());
			}
		}
		catch(Exception e)
		{
			LoggerFactory.getLog().warn(e.getMessage(), e);
		}
		
		return SNS_RESPONSE_FOR_FORGOTPSW_ERROR_TEMPLATE;
	}
	/**
	 * 返回填充了内容之后的服务端响应用户“忘记密码”请求（没有查到账户信息）的邮件模板.
	 * 
	 * @param fromMail 模板第1个参数是：忘记密码请求的目标邮箱地址
	 * @return
	 */
	public String getSNS_RESPONSE_FOR_FORGOTPSW_ERROR_TEMPLATE(String fromMail)
	{
		String contentTemplate = getSNS_RESPONSE_FOR_FORGOTPSW_ERROR_TEMPLATE();
		if(contentTemplate != null)
			return MessageFormat.format(contentTemplate
					,new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()), fromMail);
		return null;
	}
	
	public String getDIR_USER_AVATAR_UPLOAD()
	{
		return getPropertyUTF("DIR_USER_AVATAR_UPLOAD");
	}
	public String getDIR_USER_AVATARGROUP_DIR()
	{
		return getPropertyUTF("DIR_USER_AVATARGROUP_DIR");
	}
	
	public String getDIR_USER_IMAGE_UPLOAD_UNREAD()
	{
		return getPropertyUTF("DIR_USER_IMAGE_UPLOAD_UNREAD");
	}
	public String getDIR_USER_IMAGE_UPLOAD_READ()
	{
		return getPropertyUTF("DIR_USER_IMAGE_UPLOAD_READ");
	}
	public String getDIR_USER_IMAGE_UPLOAD_TIMEOUT()
	{
		return getPropertyUTF("DIR_USER_IMAGE_UPLOAD_TIMEOUT");
	}
	
	public String getDIR_USER_VOICE_UPLOAD_UNREAD()
	{
		return getPropertyUTF("DIR_USER_VOICE_UPLOAD_UNREAD");
	}
	public String getDIR_USER_VOICE_UPLOAD_READ()
	{
		return getPropertyUTF("DIR_USER_VOICE_UPLOAD_READ");
	}
	public String getDIR_USER_VOICE_UPLOAD_TIMEOUT()
	{
		return getPropertyUTF("DIR_USER_VOICE_UPLOAD_TIMEOUT");
	}
	public String getDIR_USER_VOICE_UPLOAD_TO_MP3()
	{
		return getPropertyUTF("DIR_USER_VOICE_UPLOAD_TO_MP3");
	}
	
	public String getDIR_USER_BIGFILE_UPLOAD()
	{
		return getPropertyUTF("DIR_USER_BIGFILE_UPLOAD");
	}
	
	public String getDIR_USER_SHORTVIDEO_UPLOAD()
	{
		return getPropertyUTF("DIR_USER_SHORTVIDEO_UPLOAD");
	}
	public String getDIR_USER_SHORTVIDEO_UPLOAD_TIMEOUT()
	{
		return getPropertyUTF("DIR_USER_SHORTVIDEO_UPLOAD_TIMEOUT");
	}
	
	public String getAUTO_TIMING_RESTORE_IMG_AND_VOICE_INTERVAL()
	{
		return getPropertyUTF("AUTO_TIMING_RESTORE_IMG_AND_VOICE_INTERVAL");
	}
	public String getAUTO_TIMING_RESTORE_IMG_AND_VOICE_TIMEOUT()
	{
		return getPropertyUTF("AUTO_TIMING_RESTORE_IMG_AND_VOICE_TIMEOUT");
	}
	
	public String getDIR_USER_PHOTO_UPLOAD()
	{
		return getPropertyUTF("DIR_USER_PHOTO_UPLOAD");
	}
	public String getDIR_USER_PVOICE_UPLOAD()
	{
		return getPropertyUTF("DIR_USER_PVOICE_UPLOAD");
	}
	public String getDIR_USER_LOCATION_UPLOAD_UNREAD()
	{
		return getPropertyUTF("DIR_USER_LOCATION_UPLOAD_UNREAD");
	}
	public String getDIR_USER_LOCATION_UPLOAD_READ()
	{
		return getPropertyUTF("DIR_USER_LOCATION_UPLOAD_READ");
	}
	public String getDIR_USER_LOCATION_UPLOAD_TIMEOUT()
	{
		return getPropertyUTF("DIR_USER_LOCATION_UPLOAD_TIMEOUT");
	}
	public String getDIR_USER_REMARK_PHOTO_UPLOAD()
	{
		return getPropertyUTF("DIR_USER_REMARK_PHOTO_UPLOAD");
	}

	public boolean getAPPKEY_VERIFY_ENABLED()
	{
		return Boolean.valueOf(getPropertyUTF("APPKEY_VERIFY_ENABLED"));
	}
	public String getAPPKEY_GRANTED()
	{
		return getPropertyUTF("APPKEY_GRANTED");
	}

	public boolean getTOKEN_FOR_HTTP_VERIFY_ENABLED()
	{
		return Boolean.valueOf(getPropertyUTF("TOKEN_FOR_HTTP_VERIFY_ENABLED"));
	}
	public boolean getTOKEN_FOR_FILE_UPLOAD_VERIFY_ENABLED()
	{
		return Boolean.valueOf(getPropertyUTF("TOKEN_FOR_FILE_UPLOAD_VERIFY_ENABLED"));
	}
	public boolean getTOKEN_FOR_SOCKET_VERIFY_ENABLED()
	{
		return Boolean.valueOf(getPropertyUTF("TOKEN_FOR_SOCKET_VERIFY_ENABLED"));
	}
	/**
	 * 是否启用的是对称加密模式的token。
	 *
	 * @return true表示是，否则不是
	 */
	public boolean isSymmetricTokenMode()
	{
		return "0".equals(getPropertyUTF("TOKEN_MODE"));
	}
	public int getTOKEN_EXPIRE_TIME()
	{
		return CommonUtils.getIntValue(getPropertyUTF("TOKEN_EXPIRE_TIME"), 0);
	}
	public String getTOKEN_SYMMETRIC_SECRET()
	{
		return getPropertyUTF("TOKEN_SYMMETRIC_SECRET");
	}
	public String getTOKEN_RSA_PRIVATE_KEY_FILE_PATH()
	{
		return getPropertyUTF("TOKEN_RSA_PRIVATE_KEY_FILE_PATH");
	}
	public String getTOKEN_RSA_PUBLIC_KEY_FILE_PATH()
	{
		return getPropertyUTF("TOKEN_RSA_PUBLIC_KEY_FILE_PATH");
	}

	public boolean getIM_BRIDGE_ENABLED()
	{
		return Boolean.valueOf(getPropertyUTF("IM_BRIDGE_ENABLED"));
	}
	public String getIM_BRIDGE_MQ_URI()
	{
		return getPropertyUTF("IM_BRIDGE_MQ_URI");
	}
	
	public int getIM_SERVER_UDP_LISTENNING_PORT()
	{
		return CommonUtils.getIntValue(getPropertyUTF("IM_SERVER_UDP_LISTENNING_PORT")
				, 9903);
	}
	public int getIM_SERVER_TCP_LISTENNING_PORT()
	{
		return CommonUtils.getIntValue(getPropertyUTF("IM_SERVER_TCP_LISTENNING_PORT")
				, 9903);
	}
	public boolean getIM_SERVER_UDP_SUPPORT()
	{
		return Boolean.valueOf(getPropertyUTF("IM_SERVER_UDP_SUPPORT"));
	}
	public boolean getIM_SERVER_TCP_SUPPORT()
	{
		return Boolean.valueOf(getPropertyUTF("IM_SERVER_TCP_SUPPORT"));
	}
	
	public String getAUTO_TIMING_RESTORE_MESSAGES_INTERVAL()
	{
		return getPropertyUTF("AUTO_TIMING_RESTORE_MESSAGES_INTERVAL");
	}
	public String getAUTO_TIMING_RESTORE_MESSAGES_TIMEOUT()
	{
		return getPropertyUTF("AUTO_TIMING_RESTORE_MESSAGES_TIMEOUT");
	}

	public boolean getAUTO_PREPARED_DEMO_DATAS(){
		return Boolean.valueOf(getPropertyUTF("AUTO_PREPARED_DEMO_DATAS"));
	}

//	public static void main(String[] args)
//	{
//		BaseConf bc = new BaseConf();
//		System.out.println(bc.getSNS_INVITE_SMTP_SERVER());
//		System.out.println(bc.getSNS_INVITE_MASTER_EMAIL_ADDR());
//		System.out.println(bc.getSNS_INVITE_MASTER_EMAIL_NAME());
//		System.out.println(bc.getSNS_INVITE_MASTER_EMAIL_PASSWORD());
//		System.out.println(bc.getSNS_INVITE_MASTER_NICK_NAME());
//		System.out.println(bc.getSNS_INVITE_MAIL_SUBJECT());
//		System.out.println(bc.getDIR_USER_AVATAR_UPLOAD());
//		
//		try
//		{
//			String s = FileHelper.readStringFromFile(
//					BaseConf.class.getResource("invite_template.html").toURI());
//			String tt = MessageFormat.format(s,"Jack Jiang","abcc@163.com","4244555441");
//			System.out.println("tt="+tt);
//		}
//		catch (Exception e)
//		{
//			e.printStackTrace();
//		}
//		BaseConf bc = new BaseConf();
//		bc.getSNS_RESPONSE_FORGOT_PSW_MAIL_SUBJECT();
//	}
}
